package org.me.marble;

import javax.microedition.khronos.opengles.GL10;

/**
 *
 * @author tungi
 */

public class AvailableObject extends GameObject{
    public Vector centre;

    protected int delay=5000;
    protected float angle=0.0f,dir_angle=0.0f,mwidth=0.5f;
    protected Vector last_marble_position=new Vector(),up,d;
    
    private int d_angle=0; 

    public AvailableObject(int id){
        super(id);
    }

    public void effect(GameRenderer _renderer,MoveMarble _mm){
        
    }

    public void setNormal(Path path){
    	Vector proj=null;
    	Vector direction=new Vector();
    	Vector v=new Vector(centre);v.setY(0);
        for(int i=0;i<path.triangles.size();++i){
            if(path.triangles.get(i).isPartProj(v)){
                if(i%2==0)
                    direction=Vector.Minus(
                            Vector.Plus(path.triangles.get(i+1).getPoint(0),
                                    path.triangles.get(i+1).getPoint(2)),
                            Vector.Plus(path.triangles.get(i).getPoint(2),
                                    path.triangles.get(i).getPoint(0)));
                else
                    direction=Vector.Minus(
                            Vector.Plus(path.triangles.get(i).getPoint(0),
                                    path.triangles.get(i).getPoint(2)),
                            Vector.Plus(path.triangles.get(i-1).getPoint(2),
                                    path.triangles.get(i-1).getPoint(0)));
                direction.Normalize();
                up=path.triangles.get(i).getNormal();
                proj=Geometry.getProjectionToPlane(up,new Vector(),up);
                break;
            }
        }
        dir_angle=-MyMath.RadtoDegree(Math.acos(Geometry.getCosAngle(
        		new Vector(direction.getX(),0,direction.getZ()),new Vector(0,0,-1))));
        d=Geometry.rotateVector(Math.PI/2,proj,up);
        //if vector is a null vector (this means, that the plane normal is (0,1,0))
        //the normalize operation give us a (NaN,NaN,NaN) vector, which unusable
        if(!Vector.Equal(d,new Vector()))d.Normalize();
        angle=MyMath.RadtoDegree(Math.acos(Geometry.getCosAngle(up,new Vector(0,1,0))));
    }

    public int pickedUp(Vector marble_position){
        Vector marble_move=Vector.Minus(marble_position,last_marble_position);
        Vector delta=Vector.Minus(marble_position,centre);
        if(Geometry.getCosAngle(marble_move,delta)>0){
                last_marble_position.set(marble_position.getX(),
                        marble_position.getY(),marble_position.getZ());
                return availableObjects.missed.ordinal();
        }
        else if(delta.Length()<mwidth)
            return availableObjects.pickedup.ordinal();
        else{
            last_marble_position.set(marble_position.getX(),
                    marble_position.getY(),marble_position.getZ());
            return availableObjects.none.ordinal();
        }
    }

    public void transform(GL10 gl){
        gl.glTranslatef(centre.getX(),centre.getY(),centre.getZ());
        gl.glRotatef(d_angle,up.getX(),up.getY(),up.getZ());
        gl.glRotatef(angle,d.getX(),d.getY(),d.getZ());
    }

    public void update(){
        d_angle=d_angle<360?d_angle+5:0;
    }

    public Vector getUp(){
        return up;
    }

    public Vector getCentre(){
        return centre;
    }
}
